using System;
using System.Configuration;
using System.Reflection;

namespace BoxBinary.AspectCache.Core.ErrorLogging
{
    public class ConfigSection : ConfigurationSection
    {
        private static ConfigSection _section;

        public static ConfigSection Current
        {
            get
            {
                if (_section == null)
                    _section = (ConfigSection) ConfigurationManager.GetSection("errorLoggingConfig");
                return _section;
            }
        }

        [ConfigurationProperty("sqlClientLoggerConnectionString",
            DefaultValue = "data source=ektrondev1;Trusted_Connection=Yes;initial catalog=ErrorLogger_1_0;",
            IsKey = false, IsRequired = false)]
        public string SqlClientLoggerConnectionString
        {
            get { return ((string) (base["sqlClientLoggerConnectionString"])); }
            set { base["sqlClientLoggerConnectionString"] = value; }
        }

        [ConfigurationProperty("emailFromAddress",
            DefaultValue = "Website Housekeeper (Automated email) <errorlogmanager.internalsystems@condenast.co.uk>",
            IsKey = false,
            IsRequired = false)]
        public string EmailFromAddress
        {
            get { return ((string) (base["emailFromAddress"])); }
            set { base["emailFromAddress"] = value; }
        }

        [ConfigurationProperty("emailHighPrioritySystemsAddress", DefaultValue = "condenet.technical@condenast.co.uk",
            IsKey = false, IsRequired = false)]
        public string EmailHighPrioritySystemsAddress
        {
            get { return ((string) (base["emailHighPrioritySystemsAddress"])); }
            set { base["emailHighPrioritySystemsAddress"] = value; }
        }

        [ConfigurationProperty("emailHighPriorityDevelopersAddress",
            DefaultValue = "condenet.technical@condenast.co.uk",
            IsKey = false, IsRequired = false)]
        public string EmailPagerDevelopersAddress
        {
            get { return ((string) (base["emailHighPriorityDevelopersAddress"])); }
            set { base["emailHighPriorityDevelopersAddress"] = value; }
        }

        [ConfigurationProperty("emailLowPriorityDevelopersAddress",
            DefaultValue = "OnlineDevelopmentTeam@condenast.co.uk",
            IsKey = false, IsRequired = false)]
        public string EmailLowPriorityDevelopersAddress
        {
            get { return ((string) (base["emailLowPriorityDevelopersAddress"])); }
            set { base["emailLowPriorityDevelopersAddress"] = value; }
        }

        [ConfigurationProperty("errorLoggingProvider",
            DefaultValue =
                "CondeNetUK.FoundationServices.OperationalManagement.ErrorLogging.SqlClientLogger, CondeNetUK.FoundationServices"
            ,
            IsKey = false, IsRequired = false)]
        public string ErrorLoggingProvider
        {
            get { return ((string) (base["errorLoggingProvider"])); }
            set { base["errorLoggingProvider"] = value; }
        }

        [ConfigurationProperty("isLoggingEnabled", DefaultValue = true, IsKey = false, IsRequired = false)]
        public bool IsLoggingEnabled
        {
            get { return ((bool) (base["isLoggingEnabled"])); }
            set { base["isLoggingEnabled"] = value; }
        }

        public static ILogProvider GetSelectedLogProvider()
        {
            ILogProvider _iLog = null;
            if (Current != null && Current.ErrorLoggingProvider != null)
            {
                string strDelegate = Current.ErrorLoggingProvider.Split(',')[0];
                string strAssembly = Current.ErrorLoggingProvider.Split(',')[1];
                Assembly asm = Assembly.Load(strAssembly.Trim());
                Type t = asm.GetType(strDelegate.Trim());
                if (t != null)
                {
                    if (t.GetInterface(typeof (ILogProvider).FullName) == null)
                    {
                        throw new Exception(
                            "Logging delegate must implement the interface CondeNetUK.FoundationServices.OperationalManagement.ErrorLogging.ILogProvider");
                    }
                }
                else
                {
                    throw new Exception(
                        string.Format("Logging delegate type '{0}' not found in assembly '{1}'.", strDelegate,
                                      strAssembly));
                }
                _iLog = (ILogProvider) Activator.CreateInstance(t);
            }
            else
                _iLog = new EmailErrorLogger();
            return _iLog;
        }
    }
}